import { post } from '../util/proxy'
// var mHost = "http://127.1.0.1:9002"
var mHost = "http://localhost:9002"
    // var mFileHost = mHost + "/storage/create"
var mFileHost = "http://localhost:9002/storage/create"
var mFileServer = "http://localhost:8080/Files"
export function getShopidApi() {
    return 335765716
}

export function getRequestHeader() {
    var header = {};
    header.host = mHost;
    header.fileHost = mFileHost;
    header.fileServer = mFileServer;
    header.token = getCookie("tokenx");
    // alert(header.token)
    return header;
}

export function updateToken() {
    var token = getQueryVariable("token");
    if (token) {
        setCookie("tokenx", token);
    }
    return token
}

export function loginApi(username, pass, vertifiCode, tag, header, callback) {
    $.ajax({
        url: header.host + '/user/login',
        type: "post",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        async: true,
        data: JSON.stringify({
            "userId": username,
            "password": pass,
            "vertifiCode": vertifiCode,
            "tag": tag,
        }),
        success: function(data) {
            console.log(JSON.stringify(data, null, 2))
            if (data.code == 200) {
                var token = data.data.token
                setCookie("tokenx", token)
                callback.success(data.data)
            } else {
                callback.fail(data.msg)
            }
        },
        error: function(xhr, errorType, error) {
            //   console.log("网络错误")
            callback.fail("网络错误")
        }
    });
}


export function checkLoginStatusApi(header, callback) {
    if (!header.token) {
        callback.fail("未登录")
        return
    }
    $.ajax({
        url: header.host + '/user/checkLoginStatus',
        type: "post",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        async: false,
        data: JSON.stringify({
            "token": header.token
        }),
        success: function(data) {
            console.log(JSON.stringify(data, null, 2))
            if (data.code == 200) {
                callback.success(data.data)
            } else {
                callback.fail(data.msg)
            }
        },
        error: function(xhr, errorType, error) {
            //   console.log("网络错误")
            callback.fail("网络错误")
        }
    });
}

export function getThirdCompanyApi(header, callback) {

    $.ajax({
        url: header.host + '/user/getThirdCompany',
        type: "post",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        async: false,
        data: JSON.stringify({
            "token": header.token
        }),
        success: function(data) {
            console.log(JSON.stringify(data, null, 2))
            if (data.code == 200) {
                callback.success(data.data)
            } else {
                callback.fail(data.msg)
            }
        },
        error: function(xhr, errorType, error) {
            //   console.log("网络错误")
            callback.fail("网络错误")
        }
    });
}



// export function loginoutApi() {
//     setCookie("tokenx", "");
// }

export function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) { return pair[1]; }
    }
    return (false);
}


export function setCookie(name, value) {
    //设置名称为name,值为value的Cookie
    // var expdate = new Date(); //初始化时间
    // expdate.setTime(expdate.getTime() + 300 * 30 * 3600 * 1000); //时间单位毫秒
    // document.cookie = name + "=" + value + ";expires=" + expdate.toGMTString() + ";path=/";

    //即document.cookie= name+"="+value+";path=/";  时间默认为当前会话可以不要，但路径要填写，因为JS的默认路径是当前页，如果不填，此cookie只在当前页面生效！

    var storage = window.localStorage;
    storage[name] = value;
}


export function getCookie(c_name) {
    //判断document.cookie对象里面是否存有cookie
    // if (document.cookie.length > 0) {
    //     var c_start = document.cookie.indexOf(c_name + "=")
    //         //如果document.cookie对象里面有cookie则查找是否有指定的cookie，如果有则返回指定的cookie值，如果没有则返回空字符串
    //     if (c_start != -1) {
    //         c_start = c_start + c_name.length + 1
    //         var c_end = document.cookie.indexOf(";", c_start)
    //         if (c_end == -1) c_end = document.cookie.length
    //         return unescape(document.cookie.substring(c_start, c_end))
    //     }
    // }
    // return ""
    var storage = window.localStorage;
    var value = storage[c_name];
    if (!value || value.length == 0) {
        return ""
    }
    return value
}




export function updateUserApi(user, header, callback) {

    $.ajax({
        url: header.host + '/user/updateUser',
        type: "post",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        async: false,
        data: JSON.stringify({
            "token": header.token,
            "user": user
        }),
        success: function(data) {
            console.log(JSON.stringify(data, null, 2))
            if (data.code == 200) {
                callback.success(data.data)
            } else {
                callback.fail(data.msg)
            }
        },
        error: function(xhr, errorType, error) {
            //   console.log("网络错误")
            callback.fail("网络错误")
        }
    });
}


export function addUserApi(name, tel, roles, partmentId, header, callback) {

    $.ajax({
        url: header.host + '/user/addUser',
        type: "post",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        async: false,
        data: JSON.stringify({
            "token": header.token,
            "user": {
                "name": name,
                "tel": tel,
                "partmentId": partmentId,
                "roles": roles,
            }
        }),
        success: function(data) {
            console.log(JSON.stringify(data, null, 2))
            if (data.code == 200) {
                callback.success(data.data)
            } else {
                callback.fail(data.msg)
            }
        },
        error: function(xhr, errorType, error) {
            //   console.log("网络错误")
            callback.fail("网络错误")
        }
    });
}
export function getUserDetailApi(header, callback) {

    $.ajax({
        url: header.host + '/user/getUserDetail',
        type: "post",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        async: false,
        data: JSON.stringify({
            "token": header.token,
        }),
        success: function(data) {
            if (data.code == 200) {
                callback.success(data.data)
            } else {
                callback.fail(data.msg)
            }
        },
        error: function(xhr, errorType, error) {
            //   console.log("网络错误")
            callback.fail("网络错误")
        }
    });
}

export function mdfyPassApi(newPass, oldPass, header, callback) {

    $.ajax({
        url: header.host + '/user/mdfyPass',
        type: "post",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        async: false,
        data: JSON.stringify({
            "token": header.token,
            "newPass": newPass,
            "oldPass": oldPass
        }),
        success: function(data) {
            if (data.code == 200) {
                callback.success(data.data)
            } else {
                callback.fail(data.msg)
            }
        },
        error: function(xhr, errorType, error) {
            //   console.log("网络错误")
            callback.fail("网络错误")
        }
    });
}


export function getAllReviewersApi(header, callback) {

    $.ajax({
        url: header.host + '/user/getAllReviewers',
        type: "post",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        async: false,
        data: JSON.stringify({
            "token": header.token,
        }),
        success: function(data) {
            if (data.code == 200) {
                callback.success(data.data)
            } else {
                callback.fail(data.msg)
            }
        },
        error: function(xhr, errorType, error) {
            //   console.log("网络错误")
            callback.fail("网络错误")
                // [{"label":"user","value":"用户"},{"label":"reviewer","value":"审核人"},{"label":"controller","value":"调度人"},{"label":"driver","value":"司机"}]
        }
    });
}
export function getAllUserListApi(filter, page, pagesize, header, callback) {
    var url = header.host + '/user/getAllUserList'
    var data = JSON.stringify({
        "token": header.token,
        "filter": filter,
        "page": page,
        "pagesize": pagesize,
    });
    post(url, data, callback);
}

export function loginOutApi(header, callback) {
    setCookie("tokenx", "");
    var url = header.host + '/user/loginOut'
    var data = JSON.stringify({
        "token": header.token,
    });
    post(url, data, callback);
}